package com.example.littleken.sqlitetext;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText et_name,et_age,et_id;
    private RadioGroup genderGp;
    private String genderStr = "男";//用于保存用户输入的性别
    SQLiteDatabase db;
    private ListView stuList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //数据库创建
        //String path = Environment.getExternalStorageState() + "/test.db";
        //利用SQLiteOpenHelper类打开数据库
        //利用SQLiteDatabase类来控制数据库
        SQLiteOpenHelper helper = new SQLiteOpenHelper(MainActivity.this,"test2.db",null,1) {
            @Override
            public void onCreate(SQLiteDatabase db) {
                Toast.makeText(MainActivity.this,"数据库创建",Toast.LENGTH_SHORT).show();
                //在数据库创建同时，调用表创建方法db.execSQL()表创建
                String sql = "create table info_tb (_id integer primary key autoincrement," +
                        "name varhcar(20)," +
                        "age integer, "+
                        "gender varhcar(4) )";
                db.execSQL(sql);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                Toast.makeText(MainActivity.this,"数据库升级",Toast.LENGTH_SHORT).show();
            }
        };

        //利用SQLiteDatabase类来控制数据库
         db = helper.getReadableDatabase();
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.name_edt);
        et_age = (EditText) findViewById(R.id.age_edt);
        et_id = (EditText) findViewById(R.id.id_edt);
        genderGp = (RadioGroup) findViewById(R.id.gender_gp);
        stuList = (ListView) findViewById(R.id.stu_list);
        genderGp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(checkedId == R.id.male){
                    genderStr = "男";
                }else{
                    genderStr = "女";
                }
            }
        });
    }

    public void operate(View v){
        String idStr = et_id.getText().toString();
        String nameStr = et_name.getText().toString();
        String ageStr = et_age.getText().toString();

        switch (v.getId()){
            case R.id.insert_btn:
                Toast.makeText(MainActivity.this,"插入",Toast.LENGTH_SHORT).show();

                //db.rawQuery()查询方法
                //db.execSQL()添加，删除，修改，创建方法

                //String sql = "insert into info_tb (name,age,gender) values ('"+nameStr+"',"+ageStr+",'"+genderStr+"')";
                //Log.e("TAG",sql);
                String sql ="insert into info_tb (name,age,gender) values (?,?,?)";
                db.execSQL(sql,new String[]{
                        nameStr,ageStr,genderStr
                });
                Log.e("TAG",sql);
                break;
            case R.id.select_btn:
                Toast.makeText(MainActivity.this,"查询",Toast.LENGTH_SHORT).show();
                String sqlSelect = "select * from info_tb";

                if(!idStr.equals("")){
                    sqlSelect += " where _id="+idStr;
                }
                Log.e("TAG",sqlSelect);
                //用Cursor接收查询结果
                Cursor c= db.rawQuery(sqlSelect,null);
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this,R.layout.item,c,new String[]{
                        "_id","name","age","gender"},new int[]{
                        R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item
                }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
                //"id","name","age","gender"的数据填入到 R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item对应的控件中
                stuList.setAdapter(adapter);
                break;
            case R.id.delete_btn:
                Toast.makeText(MainActivity.this,"删除",Toast.LENGTH_SHORT).show();
                String sqldelect = "delete from info_tb where _id=?";
                Log.e("TAG",sqldelect);
                db.execSQL(sqldelect,new String[]{
                    idStr
                });
                et_id.setText("");
                String sqlSelectsecond = "select * from info_tb";
                Cursor cc= db.rawQuery(sqlSelectsecond,null);
                SimpleCursorAdapter adaptersecond = new SimpleCursorAdapter(MainActivity.this,R.layout.item,cc,new String[]{
                        "_id","name","age","gender"},new int[]{
                        R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item
                }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
                //"id","name","age","gender"的数据填入到 R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item对应的控件中
                stuList.setAdapter(adaptersecond);
                break;
            case R.id.update_btn:
                Toast.makeText(MainActivity.this,"修改",Toast.LENGTH_SHORT).show();
                String sqlupdate = "update info_tb set name=? ,age=? ,gender=? where _id=?";
                Log.e("TAG",sqlupdate);
                db.execSQL(sqlupdate,new String[]{
                        nameStr,ageStr,genderStr,idStr
                });
                et_name.setText("");
                et_age.setText("");
                et_id.setText("");
                String sqlSelectthrid = "select * from info_tb";
                Cursor ccc= db.rawQuery(sqlSelectthrid,null);
                SimpleCursorAdapter adapterthrid = new SimpleCursorAdapter(MainActivity.this,R.layout.item,ccc,new String[]{
                        "_id","name","age","gender"},new int[]{
                        R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item
                }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
                //"id","name","age","gender"的数据填入到 R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item对应的控件中
                stuList.setAdapter(adapterthrid);
            break;
        }
    }

}
